#include "swoole.h"
#include "Server.h"

int swReactor_create(swReactor *reactor, int max_event){
	int ret;

	ret = swReactorEpoll_create(reactor, max_event);

	reactor->running = 1;
	reactor->setHandle = swReactor_setHandle;

	reactor->write = swReactor_write;

	return ret;
}

int swReactor_setHandle(swReactor *reactor, int _fdtype, swReactor_handle handle){
	int fdtype = swReactor_fdtype(_fdtype);

    if (fdtype >= SW_MAX_FDTYPE)
    {
        return -1;
    }

    if (swReactor_event_read(_fdtype))
    {
        reactor->handle[fdtype] = handle;
    }
    else if (swReactor_event_write(_fdtype))
    {
        reactor->write_handle[fdtype] = handle;
    }
    else if (swReactor_event_error(_fdtype))
    {
        reactor->error_handle[fdtype] = handle;
    }
    else
    {
        return -1;
    }

    return 0;
}

int swReactor_onWrite(swReactor *reactor, swEvent *ev){
}

int swReactor_write(swReactor *reactor, int fd, void *buf, int n){
	int ret;
	swConnection *socket = swReactor_get(reactor, fd);

	printf("fd = %d ==== %d\n",socket->fd,fd);
	ret = swConnection_send(socket, buf, n, 0);

	return ret;
}
